'''
Author: xubing
Date: 2021-09-01 17:28:52
LastEditors: xubing
LastEditTime: 2021-09-07 17:55:40
FilePath: /my_gists/日期加减相关.py
'''

from datetime import datetime


class MyDateOp:
    now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print('-----------现在时间:[%s]----------------' % now_time)
    today = datetime.now().strftime('%Y-%m-%d')

    def __init__(self, op_dt=None):
        self.op_dt = op_dt if op_dt is not None else MyDateOp.today
        self.year, self.month, self.day = self.op_dt.split('-')
        print('正在操作的日期是: %s' % self.op_dt)
        print('-' * 40)

        # self.now_time = MyDateOp.now_time
        # self.yesterday_dt = self.get_yesterday_dt()
        # self.last_month_today = self.get_last_month_today(trans=False)

    def is_leap_year(self):
        '''判断操作日期的年份是否是闰年'''
        year = int(self.year)
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        elif year % 4 == 0:
            return True
        else:
            return False

    def get_yesterday_dt(self):
        year, month, day = self.year, self.month, self.day
        if day == '01':
            if month == '01':
                year, month, last_day = int(year) - 1, '13', '31'
            elif month in ['02', '04', '06', '08', '09', '11']:
                last_day = '31'
            elif month in ['05', '07', '10', '12']:
                last_day = '30'
            else:
                if self.is_leap_year() is True:
                    last_day = '29'
                else:
                    last_day = '28'
            month = int(month) - 1
            full_last_day = '{}-{:02d}-{}'.format(year, month, last_day)

        else:
            full_last_day = '{}-{}-{:02d}'.format(year, month, int(day) - 1)
        print('操作日期的昨天是: %s' % full_last_day)
        print('-' * 40)
        return full_last_day

    def get_lastmonth_today(self):
        year, month, day = self.today.split('-')
        if int(month) - 1 == 0:
            last_dt_year = int(year) - 1
            last_dt_month = 12
        else:
            last_dt_year = int(year)
            last_dt_month = int(month) - 1


if __name__ == '__main__':

    op_dt = '2021-03-01'
    MyDateOp(op_dt).get_yesterday_dt()
